package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.util.JsonResult;
import cn.stylefeng.guns.sys.modular.system.entity.BtyyCwMdpd;
import cn.stylefeng.guns.sys.modular.system.model.params.CwbbParam;
import cn.stylefeng.guns.sys.modular.system.service.BtyMdpdService;
import cn.stylefeng.guns.sys.modular.system.service.BtySyfsService;
import cn.stylefeng.guns.sys.modular.system.warpper.NoticeWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/mdpd")
public class BtyMdpdController extends BaseController {

    private String PREFIX = "/modular/system/btyMdpd/";
    @Resource
    private BtyMdpdService mdpdService;

    /**
     * 跳转到门店盘点
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "btyMdpd.html";
    }

    /**
     * 获取门店盘点列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(CwbbParam cwbbParam) {
        //获取用户的姓名
        String account = LoginContextHolder.getContext().getUser().getName();
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        Page<Map<String, Object>> list = new Page<>();
        if (account.equals("admin")|roleNames.contains("财务主管")) {
            list =this.mdpdService.list(cwbbParam);
        }else if (roleNames.contains("会计")) {
            cwbbParam.setKj(account);
            list = this.mdpdService.list(cwbbParam);
        }else if(roleNames.contains("店长报销单管理")){
            cwbbParam.setDz(account);
            list =this.mdpdService.list(cwbbParam);
        }else if (roleNames.contains("督导")) {
            cwbbParam.setDd(account);
            list = this.mdpdService.list(cwbbParam);
        }
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }
    /**
     * 获取门店盘点列表(不分页)
     */
    @RequestMapping(value = "/listExportMdpd")
    @ResponseBody
    public Object listExportMdpd(CwbbParam cwbbParam) {
        //获取用户的姓名
        String account = LoginContextHolder.getContext().getUser().getName();
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        List<BtyyCwMdpd> list = null;
        if (account.equals("admin")|roleNames.contains("财务主管")) {
            list =this.mdpdService.listExportMdpd(cwbbParam);
        }else if (roleNames.contains("会计")) {
            cwbbParam.setKj(account);
            list = this.mdpdService.listExportMdpd(cwbbParam);
        }else if(roleNames.contains("店长报销单管理")){
            cwbbParam.setDz(account);
            list =this.mdpdService.listExportMdpd(cwbbParam);
        }else if (roleNames.contains("督导")) {
            cwbbParam.setDd(account);
            list = this.mdpdService.listExportMdpd(cwbbParam);
        }
        return list;
    }

    /**
     * 导入门店盘点excel
     */
    @RequestMapping("/importMdpd")
    @ResponseBody
    public Object fzhs(@RequestParam("file") MultipartFile file) {
        String fileName = file.getOriginalFilename();
        JsonResult jsonResult = new JsonResult();
        Boolean result;
        try {
            result = this.mdpdService.batchImportMdpd(fileName, file);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(0);
            jsonResult.setErrmsg(e.getMessage());
            return jsonResult;
        }
        return result;
    }



}